//
//  AddTitpTittleViewController.m
//  TipsProject
//
//  Created by Suny on 15/5/19.
//  Copyright (c) 2015年 Suny. All rights reserved.
//

#import "AddTitpTittleViewController.h"
#import "UIBarButtonItem+BaseFramework.h"
#import "AddTipContentViewController.h"
#import "LYTextView.h"

@interface AddTitpTittleViewController ()
@property (nonatomic, strong) UILabel *tipLabel;
@end

@implementation AddTitpTittleViewController

- (void)viewDidLoad
{
	[super viewDidLoad];
	self.navigationItem.leftBarButtonItem = self.defaultBackButtonItem;
	self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightBarButtonItemWithTitle:@"下一步"
																				   target:self
																				 selector:@selector(handleGoToNext)];

	[self setNavigationTitle: @"添加标题"];
	
	

	[self.view addSubview:self.textView];
	[self.view addSubview:self.tipLabel];
	
	self.tipLabel.right = self.textView.right;
	self.tipLabel.top = self.textView.bottom + 20;
	
	
}


- (UILabel *)tipLabel
{
	if (!_tipLabel)
	{
		_tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
		_tipLabel.backgroundColor = [UIColor clearColor];
		_tipLabel.textColor = [UIColor redColor];
		_tipLabel.text = @"100字";
		
		_tipLabel.textAlignment = NSTextAlignmentRight;
	}
	
	return _tipLabel;
}

- (UITextView *)textView
{
    if (!_textView)
    {
        _textView = [[LYTextView alloc] initWithFrame:CGRectMake(10, self.navigationBarHeight + 20, self.view.width - 20, 40)];
        _textView.layer.borderWidth = 1;
        _textView.placeholder =  @"输入标题";
        _textView.text = self.editText;
        _textView.font = [UIFont systemFontOfSize:20];
        _textView.returnKeyType = UIReturnKeyNext;
		_textView.delegate = self;
		_textView.layer.borderColor = [UIColor colorWithHexString:@"E6E6E6"].CGColor;
		_textView.layer.borderWidth = 4;
    }
    return _textView;
}

- (void)textViewDidChange:(UITextView *)textView;
{
	CGFloat width = self.textView.width;
	CGFloat maxHeight = 150;
	if(self.textView.height != maxHeight)
	{
		[self.textView sizeToFit];
		
		self.textView.width = width;
		
		self.textView.height = self.textView.height > maxHeight ? maxHeight :self.textView.height;
		
		self.tipLabel.top = self.textView.bottom + 20;
	}


	
}
- (void)handleGoToNext
{
    if (CHECK_VALID_STRING(_textView.text))
    {
        if (self.finishBlock)
        {
            self.finishBlock(_textView.text);
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            AddTipContentViewController *addTipContentViewController = [[AddTipContentViewController alloc] init];
            addTipContentViewController.topicInfo = self.topicInfo;
            addTipContentViewController.tipTitle = _textView.text;
            [self.navigationController pushViewController:addTipContentViewController
                                                 animated:YES];
        }

    }
    else
    {
        [self showAlertMessage:@"请填写标题！"];
    }

}
@end
